package com.cotx.keypairs;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;


public class KeyPairTools {
	public static final String KEY_ALGORITHM = "RSA";
	private KeyPair pair;
	
	public void KeyPairInit() {
		KeyPairGenerator keyGen;
		try {
			keyGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
			keyGen.initialize(1024);
			pair = keyGen.generateKeyPair();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getPublicKey() {
		PublicKey pubKey = pair.getPublic();
		return Base64.getEncoder().encodeToString(pubKey.getEncoded());
	}
	
	public String getPrivateKey() {
		PrivateKey privKey = pair.getPrivate();
		return Base64.getEncoder().encodeToString(privKey.getEncoded());
	}
	
	public static void main(String[] args) {
		KeyPairTools tools = new KeyPairTools();
		tools.KeyPairInit();
		System.out.println("Generate key pair:");
		System.out.println("Public Key:");
		System.out.println(tools.getPublicKey());
		System.out.println("Private Key:");
		System.out.println(tools.getPrivateKey());
	}
}
